<?php
namespace app\beautiful\controller\admin\appointment;
use app\BaseController;
use app\beautiful\model\appointment\PerformanceTypeModel;
use app\common\logic\DataLogic;

class PerformanceType extends BaseController{
    protected $name="业绩填单类型";

    public function index()
    {
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        $performance_type_model=new PerformanceTypeModel();
        $order=['add_time'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($performance_type_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function show()
    {
        $performance_type_id=$this->request->param('performance_type_id/d',0);
        if($performance_type_id==0) return $this->success([]);

        if(null === $performance_type = PerformanceTypeModel::find($performance_type_id)) return $this->error($this->name.'不存在');
        if($performance_type->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($performance_type->is_delete != 0) return $this->error($this->name.'不存在');
        $form=[
            'name'=>$performance_type->name,
            'single_commission'=>$performance_type->single_commission
        ];
        return $this->success(['form'=>$form]);
    }

    public function save()
    {
        $performance_type_id=$this->request->param('performance_type_id/d',0);
        $name=$this->request->param('name');
        $single_commission=$this->request->param('single_commission/f',0);

        if(empty($name)) return $this->error('分类名称必须填写');
        if(empty($single_commission) || $single_commission<=0) return $this->error('请输入单次提成,且不能小于0');

        if($performance_type_id != 0){
            if(null === $performance_type = PerformanceTypeModel::find($performance_type_id)) return $this->error($this->name.'不存在');
            if($performance_type->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
            if($performance_type->is_delete != 0) return $this->error($this->name.'不存在');
            if($name != $performance_type->name){
                if(null !== PerformanceTypeModel::where([
                        ['shop_id','=',$this->request->shopId],
                        ['is_delete','=',0],
                        ['name','=',$name]
                    ])->find()){
                    return $this->error('名称已有，请换一个');
                }
            }
        }else{
            $performance_type = new PerformanceTypeModel();
            $performance_type->shop_id=$this->request->shopId;
            if(null !== PerformanceTypeModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['is_delete','=',0],
                    ['name','=',$name]
                ])->find()){
                return $this->error('名称已有，请换一个');
            }
        }

        $performance_type->name=$name;
        $performance_type->single_commission=$single_commission;
        $performance_type->save();
        return $this->success('保存成功');
    }

    public function del()
    {
        $performance_type_id=$this->request->param('performance_type_id/d',0);
        if(null === $performance_type = PerformanceTypeModel::find($performance_type_id)) return $this->error($this->name.'不存在');
        if($performance_type->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($performance_type->is_delete != 0) return $this->error($this->name.'不存在');
        $performance_type->is_delete=1;
        $performance_type->save();
        return $this->success('删除成功');
    }
}
